package cn.skyclass.beans
{
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	
	/**
	 * 基本的树节点信息，主要实现 ITreeNode 接口，另外提供一些常用的节点操作方法
	 */
	[Bindable]
	public class TreeNodeFolderBean extends BaseExBean implements ITreeNode
	{				
		// # implementing ITreeNode
	
		public function getIconOpen():Class{
			return null;
		}	
			
		public function getIconClose():Class{
			return null;
		}
		
		// parent property
		private var _parent:ITreeNode;
		[Transient]
		public function get parent():ITreeNode{
			return this._parent;
		}	
		public function set parent(value:ITreeNode):void{
			this._parent = value;
		}
			
		// children property
		private var _children:ArrayCollection = new ArrayCollection();
		[Transient]
		public function get children():ArrayCollection{
			return this._children;
		}				
		
		// expanded property
		private var _expanded:Boolean;
		[Transient]
		public function get expanded():Boolean{
			return this._expanded;
		}	
		public function set expanded(value:Boolean):void{
			this._expanded = value;
		}
		
		public function addChild(child:ITreeNode):Boolean{
			if(child == null){
				return false;
			}else{			
				this.children.addItem(child);
				child.parent = this;	
				return true;
			}
		}
		
		public function removeChild(child:ITreeNode):Boolean{
			var index:int = this.children.getItemIndex(child);
			if(index < 0){
				return false;
			}else{
				this.children.removeItemAt(index);
				child.parent = null;
				return true;
			}
		}

		/**
		 * 依据 id 的值对所有的子目录进行排序
		 */
		public function sortChildren():void{	
			var s:Sort = new Sort();
			s.fields = [new SortField("id")];
			this.children.sort = s;
			this.children.refresh();
		}	
			
		// # ITreeNode end
		
		
		/**
		 * 获取此节点的根节点
		 */
		public function root():ITreeNode{			
			var temp:ITreeNode = this;
			while(temp.parent){				
				temp = temp.parent;
			}			
			return temp;
		}	
		
		/**
		 * 此节点是否是根节点
		 */
		public function isRoot():Boolean{
			return this.parent == null;
		}
		
		/**
		 * 此节点是否有子节点
		 */
		public function hasChildren():Boolean{
			return this.children && this.children.length > 0;
		}
				
		/**
		 * 节点的层次深度，根节点为 0, 一级节点为 1, ......
		 */
		public function depth():int{
			var d:int = 0;
			var temp:ITreeNode = this;
			while(temp.parent){				
				temp = temp.parent;
				d++;
			}		
			return d;				
		}
			
		/**
		 * 重置所有的子节点
		 */
		public function resetChildrenWith(newValues:ArrayCollection):void{
			this.children.removeAll();
			for each(var o:Object in newValues){				
				this.addChild(o as ITreeNode);
			}			
		}
		
		/**
		 * 添加多个孩子节点
		 */
		public function addChildren(children:ArrayCollection):void{
			for each(var o:Object in children){				
				this.addChild(o as ITreeNode);
			}				
		}
		
		/**
		 * 获取节点的全名称，从根节点开始的节点名序列
		 */
		public function getFullName(separator:String=" -> "):String{
			var cs:Array = new Array();
			var temp:ITreeNode = this;
			while(temp){
				cs.push(temp.label);
				temp = temp.parent;
			}
			cs.reverse();
			return cs.join(separator);	
		}
		
					
	}
}